package com.timepies.activityutils;

import java.util.List;

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.timepies.R;
import com.timepies.activity.ElencoPieActivity;

public class ElencoLeftMenu implements OnItemClickListener {

	private ListView list;
	private DrawerLayout drawerLayout;
	private ActionBarDrawerToggle actionBarDrawerToggle;
	private ElencoPieActivity activity;

	public ElencoLeftMenu(ElencoPieActivity activity, List<String> itemsmenu) {
		this.activity = activity;

		Log.d(this.toString(), "LeftMenu constructor!");

		list = (ListView) activity.findViewById(R.id.left_drawer);

		list.setAdapter(new ArrayAdapter<String>(activity,
				R.layout.common_menu_items, R.id.title, itemsmenu));

		drawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);

		actionBarDrawerToggle = new ActionBarDrawerToggle(activity,
				drawerLayout, R.drawable.ic_launcher, R.string.drawer_open,
				R.string.drawer_close);

		drawerLayout.setDrawerListener(actionBarDrawerToggle);

		activity.getActionBar().setDisplayHomeAsUpEnabled(true);

		list.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Log.d(this.toString(), "p:" + position + " id:" + id);
		activity.getElencoPieList().load(position);
		drawerLayout.closeDrawer(list);

	}

	public void select(MenuItem item) {
		actionBarDrawerToggle.onOptionsItemSelected(item);

	}

}
